﻿using System;
using System.Collections.Generic;
using System.Text;
using VideoCache.Aggregation.Interfaces;
using Fsb.Utility;
using System.Text.RegularExpressions;

namespace VideoCache.Aggregation.Implements
{
    [Site("funshion.com")]
    class FunsionFetcher
    {
        public string GetToken(string url)
        {
            string token = this.GetTokenByUrl(url);

            if (string.IsNullOrEmpty(token))
            {
                return this.GetTokenByPlayer(url);
            }

            return token;
        }

        string GetTokenByUrl(string url)
        {
            // http://www.funshion.com/subject/play/100273/1

            string pattern = "^www.funshion.com/subject/play/(?<AlbumId>.*)/(?<DataId>.*)";

            Match match = Regex.Match(url, pattern);

            if (match.Success)
            {
                return match.Groups["AlbumId"].Value + "_" + match.Groups["DataId"].Value;
            }

            return null;
        }

        string GetTokenByPlayer(string url)
        {
            //pauseAp=86277BA3_1CF2_EF57_251A_0C96EA20869D&mediaAp=c_wb_1_lv&userSeek=1&type=movie&partner=155085&funshionSetup=0&start=1&itemid=100273&data=2&startAd=1&userMac=096D26EE125F6006ADB5344A2D40BE4C&poster=&stopUrl=http%3A%2F%2Fstatic.funshion.com%2Fmain%2F201304160908%2Fswf%2FwwwAppPlayerStopLayoutWww.swf

            string albumId = UrlHelper.GetParameter(url, "itemid");

            if (!string.IsNullOrEmpty(albumId))
            {
                return albumId + "_" + UrlHelper.GetParameter(url, "data");
            }

            return null;
        }
    }
}
